package com.service.impl;

import com.mapper.DepartmentMapper;
import com.mapper.EmployeeMapper;
import com.pojo.Department;
import com.pojo.Employee;
import com.pojo.view.StaffAnnualLeave;
import com.service.EmployeeService;
import com.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class EmployeeServiceImpl implements EmployeeService {

    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public String getDepartmentByUsername(String username) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        Employee employee = mapper.getByUsername(username);
        int deptId = employee.getDeptId();
        DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
        Department department = departmentMapper.getById(deptId);

        sqlSession.close();
        return department.getDeptName();
    }

    @Override
    public String getDepartmentByWorkId(String workId) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        Employee employee = employeeMapper.getByWorkId(workId);
        int deptId = employee.getDeptId();
        DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
        Department department = departmentMapper.getById(deptId);

        sqlSession.close();
        return department.getDeptName();
    }

    @Override
    public void setAnnualLeaveTotalByWorkId(String workId, int annualLeaveTotal) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        mapper.setAnnualLeaveTotal(workId, annualLeaveTotal);

        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void setAnnualLeaveRemainByWorkId(String workId, int reduceDay) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        mapper.setAnnualLeaveRemain(workId, reduceDay);

        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void addEmployee(Employee employee) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        String name = employee.getName();
        String workId = employee.getWorkId();
        String phoneNumber = employee.getPhoneNumber();
        int deptId = employee.getDeptId();
        mapper.addEmployee(name, workId, phoneNumber, deptId);

        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public int getAnnualLeaveRemainByUsername(String username) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        Employee employee = mapper.getByUsername(username);

        int annualLeaveRemain = employee.getAnnualLeaveRemain();
        sqlSession.close();
        return annualLeaveRemain;
    }

    @Override
    public int getLeaveTotalByUsername(String username) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        Employee employee = mapper.getByUsername(username);

        int leaveTotal = employee.getLeaveTotal();
        sqlSession.close();
        return leaveTotal;
    }

    @Override
    public int getOutTotalByUsername(String username) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        Employee employee = mapper.getByUsername(username);

        int outTotal = employee.getOutTotal();
        sqlSession.close();
        return outTotal;
    }

    @Override
    public List<Employee> getAllEmployee() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        List<Employee> employeeList = mapper.selectAll();

        sqlSession.close();
        return employeeList;
    }

    @Override
    public Employee getEmployeeByUsername(String username) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        Employee employee = mapper.getByUsername(username);

        sqlSession.close();
        return employee;
    }
}
